package mylab.starters.uuid.impl.redis;

import mylab.starters.uuid.AbstractUuidTemplate;
import mylab.starters.uuid.Uuid;
import mylab.starters.uuid.UuidProvider;
import mylab.starters.uuid.config.UuidProperties;
import mylab.starters.uuid.model.BusinessName;
import mylab.starters.uuid.model.Prefix;
import mylab.starters.uuid.model.impl.DefaultBusinessName;
import mylab.starters.uuid.model.impl.DefaultPrefix;
import mylab.starters.uuid.model.impl.DefaultRangeId;
import mylab.starters.uuid.model.impl.RedisRangeManager;
import mylab.utils.spring.spi.SpiProvider;

public class RedisUuidTemplate extends AbstractUuidTemplate<UuidProperties.ReidsUuidProperties> {

    /**
     * 连接redis的IP[必选]
     */
    private String ip;
    /**
     * 连接redis的port[必选]
     */
    private int port;
    /**
     * 业务名称[必选]
     */
    private BusinessName businessName;
    /**
     * 主键前缀[可选：默认为空]
     */
    private Prefix prefix = new DefaultPrefix();
    /**
     * 密码[可选]
     */
    private String password;
    /**
     * 获取range步长[可选，默认：1000]
     */
    private int step = 1000;

    /**
     * 序列号分配起始值[可选：默认：0]
     */
    private long stepStart = 0;


    public RedisUuidTemplate() {
    }

    public RedisUuidTemplate(UuidProperties.ReidsUuidProperties properties) {
        super(properties);
        this.ip = properties.getIp();
        this.port = properties.getPort();
        this.businessName = new DefaultBusinessName(properties.getBusinessName());
        this.prefix = new DefaultPrefix(properties.getPrefix());
        this.password = properties.getPassword();
        this.step = properties.getStep();
        this.stepStart = properties.getStepStart();
    }


    public static RedisUuidTemplate create() {
        return new RedisUuidTemplate();
    }

    public RedisUuidTemplate ip(String ip) {
        this.ip = ip;
        return this;
    }

    public RedisUuidTemplate port(int port) {
        this.port = port;
        return this;
    }

    public RedisUuidTemplate password(String password) {
        this.password = password;
        return this;
    }

    public RedisUuidTemplate step(int step) {
        this.step = step;
        return this;
    }

    public RedisUuidTemplate businessName(BusinessName businessName) {
        this.businessName = businessName;
        return this;
    }

    public RedisUuidTemplate prefix(Prefix prefix) {
        this.prefix = prefix;
        return this;
    }

    public RedisUuidTemplate stepStart(long stepStart) {
        this.stepStart = stepStart;
        return this;
    }

    @Override
    public Uuid getInstance() {
        //利用Redis获取区间管理器
        RedisRangeManager redisRangeManager = new RedisRangeManager();
        redisRangeManager.setIp(this.ip);
        redisRangeManager.setPort(this.port);
        redisRangeManager.setPassword(this.password);
        redisRangeManager.setStep(this.step);
        redisRangeManager.setStepStart(stepStart);
        redisRangeManager.init();
        //构建序列号生成器
        DefaultRangeId defaultRangeId = new DefaultRangeId();
        defaultRangeId.setBusinessName(this.businessName);
        defaultRangeId.setPrefix(prefix);
        defaultRangeId.setRangeManager(redisRangeManager);
        return defaultRangeId;
    }

    @Override
    public SpiProvider getProvider() {
        return UuidProvider.Redis;
    }
}
